<?php
header('Access-Control-Allow-Origin:*');
/**
 * 商会通讯录模块微站定义
 *
 */
defined('IN_IA') or exit('Access Denied');

define('BBYSCM_CARD', 'bbyscm_card');

define('BBYSCM', '../addons/' . BBYSCM_CARD . '/style/');

class Bbyscm_cardModuleSite extends WeModuleSite {
  //获得分组列表
    function doMobileGetmails(){
        global $_W,$_GPC;
        $where['uniacid'] = $_GPC['i'];
        $mails = pdo_getall('bbyscm_card_mail',$where);
        die(json_encode($mails));
        // die(json_encode($_GPC));
    }
  //获得分组会员列表
    function doMobileGetcard(){
        global $_W,$_GPC;
        $where['uniacid'] = $_GPC['i'];
        $where['ai_card'] = $_GPC['pstadid'];
        $mails = pdo_getall('bbyscm_card',$where);
        die(json_encode($mails));
        // die(json_encode($_GPC));
    }
    //说明：api for 柳州加粉app 查找随机50个
    //时间：2018-08-20 10:56:33
    function doMobileApi(){
        global $_W,$_GPC;
        $cat = $_GPC['hangye'];
        $city = $_GPC['city'];
        $uuid = $_GPC['uuid'];
        if(!$cat){
            $cat = 17;
        }
        $where =" and industry=".$cat;
        $where .=" and city='".$city."'";
        $left = pdo_getcolumn('bbyscm_card',['app_uuid' => $uuid],'money');
        if($left <=0){
        $data['status'] = 0;
        $data['msg'] = '请先充值';
        $json = json_encode($data);
        die($json);
        }
        // $list = pdo_fetchall('SELECT company,cardname,mobile from ims_bbyscm_card where mobile > 10000000000 and uniacid = '.$_W['uniacid'].'  and id >= ((SELECT MAX(id) FROM ims_bbyscm_card)-(SELECT MIN(id) FROM ims_bbyscm_card)) * RAND() + (SELECT MIN(id) FROM ims_bbyscm_card) '.$where.' limit 50');
        // php写法
        // $max = pdo_count('bbyscm_card',['uniacid'=>$_W['uniacid'],'industry'=>$cat,'city'=>$city]);//总数
        // $rand = rand(0,$max-50);
        //只要姓，不要名
        // $list = pdo_fetchall('SELECT company,concat_ws("**",substring(cardname,1,1)) as cardname,mobile from ims_bbyscm_card where mobile > 10000000000 and uniacid = '.$_W['uniacid'].' and  id >='.$rand.$where.'  limit 50');
        $list = pdo_fetchall('SELECT company,cardname,mobile,job,address from ims_bbyscm_card where mobile > 10000000000 and uniacid = '.$_W['uniacid'].$where.' order by RAND()  limit 50');
        pdo_query("update ims_bbyscm_card set money = money-1 where app_uuid = '".$uuid."'");
        // var_dump($list);
        $data['list'] = $list;
        $data['status'] = 1;
        $data['note'] = '由'.$city.'加粉助手导入，与您共享全市企业商机.数据：'.count($list);//导入
        $json = json_encode($data);
        die($json);
    }
    //init
    function doMobileInit(){
        global $_W,$_GPC;
        $result['count'] = pdo_count('bbyscm_card',['uniacid'=>$_W['uniacid']]);//总数
        $uuid = $_GPC['uuid'];
        $result['money'] = pdo_getcolumn('bbyscm_card',['app_uuid'=>$uuid],'money');//余额
        $result['cats'] = pdo_getall('bbyscm_card_indus',['uniacid'=>$_W['uniacid']]);//分类
        $result['server'] = pdo_getall('bbyscm_ad',['status'=>1]);//,
        $result['note'] = '城市商机,由你创造';//
        $result['qq'] = '276601115';//
        $result['paytoken'] = '6586b1c547ff5e92c000de0b7a95f4ef';//
        $json = json_encode($result);
        die($json);
    }
    //add
    function doMobileAdd(){
        global $_W,$_GPC;
        $data['uniacid'] = 2;//死值
        $data['app_uuid'] = $_GPC['uuid'];
        $data['longitude'] = $_GPC['longitude'];
        $data['latitude'] = $_GPC['latitude'];
        $data['cardname'] = $_GPC['name'];
        $data['mobile'] = $_GPC['mobile'];
        $data['company'] = $_GPC['company'];
        $data['job'] = $_GPC['job'];
        $data['money'] = 10;//初始次数
        $data['createtime'] = time();
        $result = pdo_insert('bbyscm_card',$data);
        if($result){
            die('1');
        }else{
            die('0');
        }
    }

//说明：下单流程
//时间：2018-08-21 12:02:17
    function doMobilePay(){
        global $_W,$_GPC;
        // 处理qrcode表10分钟外为1的标记为0
        $times = time()-60*5;
        pdo_query('update ims_tompay_qrcode set status =0 where last_time <'.$times);
        // 标记order表10分钟外的status为0的为2 未支付
        pdo_query('update ims_tompay_order set status =2 where create_time <'.$times);
        // 取出一个为0的qrcode 并标记为使用中
        $qrcode = pdo_get('tompay_qrcode',['status'=>0]);
        if(!$qrcode){
            message('系统使用高峰期，请稍后充值');
        }else{
        pdo_query('update ims_tompay_qrcode set status =1 , last_time ='.time().' where id = '.$qrcode['id']);
        // 写入order新订单
        $data['uuid'] = $_GPC['uuid'];// 第三方单号
        $data['uniacid'] = 2;
        $data['money'] = $qrcode['money'];
        $data['qrcode_id'] = $qrcode['id'];
        $data['pay_type'] = $qrcode['type'];
        $data['pay_note'] = $qrcode['pay_note'];
        $data['create_time'] = time();
        $result = pdo_insert('tompay_order',$data);
        if($result){
        // die();
            // 手机版则跳转
            // TODO pc则显示 qrcode
             header("location:".$qrcode['url']);
        }else{
            message('系统故障');
            // die('0');//
        }
        }
    }

    //paylist
    function doMobilePaylist(){
        global $_W,$_GPC;
        // load()->func('tpl');
        $weid=intval($_W['uniacid']);
        $op = !empty($_GPC['op']) ? $_GPC['op'] : 'display';
        if ($op == 'display') {
            $pindex = max(1, intval($_GPC['page']));
            // $sql = " where pay_class = 6";
            $psize = 10;
            $sum =pdo_fetchcolumn("SELECT sum(money) FROM ".tablename('tompay_order').$sql." limit ".$pindex * $psize, $params);
            $sum1 =pdo_fetchcolumn("SELECT sum(money) FROM ".tablename('tompay_order')."where status =1 ".$sql." limit ".$pindex * $psize, $params);
            $list = pdo_fetchall("SELECT a.* FROM " . tablename('tompay_order') . " as a WHERE a.uniacid = $weid $condition ORDER BY a.create_time DESC LIMIT " . ($pindex - 1) * $psize . ',' . $psize);
            $total = pdo_fetchcolumn('SELECT COUNT(*) FROM ' . tablename('tompay_order') . " as a WHERE a.uniacid = $weid  $condition");
            // var_dump($list);
            $pager = pagination($total, $pindex, $psize);
        } elseif($op == 'change') {
            $id=intval($_GPC['id']);
            $status=$_GPC['status'];
            if($id){
                pdo_update('tompay_order', array('status'=>$status), array('id'=>$id));
                $order = pdo_get('tompay_order',['id'=>$id]);
                pdo_query("update ims_bbyscm_card set money = money+".ceil($order['money'])." where app_uuid = '".$order['uuid']."'");
            }
            message('操作成功！', $this->createMobileUrl('paylist', array('op'=>'display')), 'success');
        } elseif($op == 'del') {
            $id=intval($_GPC['id']);
            if($id){
                pdo_delete('tompay_order', array('id' => $id));
            }
            message('操作成功！', $this->createMobileUrl('paylist', array('op'=>'display')), 'success');
        }
        include $this->template('paylist');
    }
    //更新城市
    function doMobileUpdatecity(){
        global $_W,$_GPC;
        $minid = $_GPC['min'];
        if($minid){
            $where = ' and id > '.$minid;
        }
        $list = pdo_fetchall('SELECT id,city,address from ims_bbyscm_card where city = "" and address != ""  and uniacid = '.$_W['uniacid'].$where.' order by id asc limit 10');
          // var_dump($list);
        // pdo_debug();
        // die();
        if($list){
            foreach($list as $key=>$val){
                if(empty($val['address'])){
                    continue;
                }
                $address = $val['address'];
                $url="http://apis.map.qq.com/ws/geocoder/v1/?address=".$address."&key=25MBZ-YMCC6-3A3S2-MUHF2-4ZJDO-HXB5K";
                $jsondata=json_decode(file_get_contents($url),true);

                $data = array(
                    'longitude' => $jsondata['result']['location']['lng'], 
                    'latitude' => $jsondata['result']['location']['lat'], 
                    'province' => $jsondata['result']['address_components']['province'],
                    'city' => $jsondata['result']['address_components']['city']?$jsondata['result']['address_components']['city']:"柳州市",
                    'county' => $jsondata['result']['address_components']['district']
                );
                pdo_update('bbyscm_card',$data,['id'=>$val['id']]);
            echo "<br>".$key."更新了".$val['id'];
            }
            // pdo_debug();
            $url = $_W['siteurl'];
            echo "<br>更新完了";

            header("refresh:2;url=$url");
            // header("location:$url");
            // message('更新了50条',"http://mo.31.vlldoo.com/app/index.php?i=2&c=entry&m=bbyscm_card&do=updatecity",'success');
        }else{
            message('全部更新完毕');
        }
    }

    //说明：邀请，TODO 有效邀请判断 
    //时间：2018-08-22 13:23:43
    function doMobileInvite(){
        global $_W,$_GPC;
        $uuid = $_GPC['uuid'];
        pdo_query("update ims_bbyscm_card set money = money+2 where app_uuid = '".$uuid."'");
        $download_url = "https://esayfamily-yhss.gitee.io/yu_general_app_downloads/liuzhoucontact/";
         header("location:$download_url");

    }

}
